package com.renwei.yunlong.pager;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.EnterpriseInformationActivity;
import com.renwei.yunlong.activity.InformationActivity;
import com.renwei.yunlong.activity.NewDetailsActivity;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.WorkInformationBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.leefeng.promptlibrary.PromptButton;
import me.leefeng.promptlibrary.PromptButtonListener;
import me.leefeng.promptlibrary.PromptDialog;

/**
 * 问题  详情  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class ProblemDetailsPager extends BaseFragment implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.rl_unit)
    RelativeLayout rlUnit;//业主单位
    @Bind(R.id.rl_initiated)
    RelativeLayout rlInitiated;//发起人
    @Bind(R.id.rl_service_new)
    RelativeLayout rlServiceNew;//服务对象

    ACache mCache;

    @Bind(R.id.tv_number)
    TextView tvNumber;
    @Bind(R.id.tv_unit)
    TextView tvUnit;
    @Bind(R.id.tv_initiated)
    TextView tvInitiated;
    @Bind(R.id.tv_deal)
    TextView tvDeal;
    @Bind(R.id.tv_deal_person)
    TextView tvDealPerson;
    @Bind(R.id.tv_service_new)
    TextView tvServiceNew;
    @Bind(R.id.tv_project)
    TextView tvProject;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.tv_schedule)
    TextView tvSchedule;
    @Bind(R.id.tv_degree)
    TextView tvDegree;
    @Bind(R.id.tv_sla)
    TextView tvSla;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_initiated_time)
    TextView tvInitiatedTime;
    @Bind(R.id.tv_complete_time)
    TextView tvCompleteTime;
    @Bind(R.id.tv_description)
    TextView tvDescription;
    @Bind(R.id.tv_detailed)
    TextView tvDetailed;

    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_problem_details, null);
        ButterKnife.bind(this, view);
        mCache = ACache.get(getActivity());
        return view;
    }

    @Override
    public void initData() {
        rlUnit.setOnClickListener(this);
        rlInitiated.setOnClickListener(this);
        rlServiceNew.setOnClickListener(this);

        String requestId = mCache.getAsString("requestId");
        if (!TextUtils.isEmpty(requestId)) {
            String jsonBean = "{requestId:\"" + requestId + "\"}";
            serviceUpcomingWorkHttp(jsonBean);
            Log.e("工单详情jsonBean", jsonBean);
        }
    }

    private void serviceUpcomingWorkHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.SERVICE_DETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void ownerUpcomingWorkHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.OWNER_DETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_unit:
                startActivity(new Intent(getActivity(), EnterpriseInformationActivity.class));
                break;
            case R.id.rl_initiated:
                startActivity(new Intent(getActivity(), InformationActivity.class));
                break;
            case R.id.rl_service_new:
                startActivity(new Intent(getActivity(), NewDetailsActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                break;
            case 2:
                Log.e("工单详情数据", result);
                WorkInformationBean workInformationBean = new Gson().fromJson(result, WorkInformationBean.class);
                if (workInformationBean.getMessage().getCode() == 200) {

                    tvNumber.setText(workInformationBean.getRows().getWorkOrder());
                    tvUnit.setText(workInformationBean.getRows().getServerName());
                    tvInitiated.setText(workInformationBean.getRows().getSendUserName());
                    tvDeal.setText(workInformationBean.getRows().getServiceProviderName());
                    tvDealPerson.setText(workInformationBean.getRows().getResponseUserName());
                    tvServiceNew.setText(workInformationBean.getRows().getHardWareName());
                    //tvProject.setText(serviceBean.getRows().getP);//所属项目

                    tvService.setText(workInformationBean.getRows().getDirectoryName());

                    //tvSchedule.setText(serviceBean.getRows().);//执行程度
                    if ("1".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("高");
                    } else if ("2".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("中");
                    } else if ("3".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("低");
                    }
                    tvSla.setText(workInformationBean.getRows().getDays() + "天" + workInformationBean.getRows().getHours() + "小时");
                    tvType.setText(workInformationBean.getRows().getSendDate());
                    if ("0".equals(workInformationBean.getRows().getContractOut())) {
                        tvType.setText("否");
                    } else if ("1".equals(workInformationBean.getRows().getContractOut())) {
                        tvType.setText("是");
                    }
                    tvInitiatedTime.setText(workInformationBean.getRows().getSendDate());
                    tvCompleteTime.setText(workInformationBean.getRows().getCompleteDate());
                    tvDescription.setText(workInformationBean.getRows().getTitle());
                    tvDetailed.setText(workInformationBean.getRows().getRequestContent());

                }
                break;
            default:
                break;

        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
